package org.example.designmodel.masterwork;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
 * 任务
 * @param <R>
 */
public class Task<R> {
    static AtomicInteger counter = new AtomicInteger(0);

    public Consumer<Task<R>> action;

    private int id;

    public int workerId;

    private R result;

    public Task() {
        this.id = counter.getAndIncrement();
    }

    public void execute() {
        this.result = doExecute();
        action.accept(this);
    }

    public R doExecute() {
        return null;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getWorkerId() {
        return workerId;
    }

    public void setWorkerId(int workerId) {
        this.workerId = workerId;
    }

    public R getResult() {
        return result;
    }
}
